<template>
  <div style="padding: 10px; width: 100%">
    <div>
      <p class="count">{{this.examInfo.examName}}成绩</p>
    </div>
    <div style="margin: 10px 0; width: 800px; float: left">
      <el-input
        placeholder="请输入学生学号"
        v-model="studentId"
        style="width: 200px"
      >
        <template #append>
          <el-button icon="el-icon-search" @click="load"></el-button>
        </template>
      </el-input>
      <span style="margin-left: 20px">班级：</span>
      <el-select
        v-model="clazzId"
        placeholder="请选择"
        style="width: 100px"
        clearable
        @change="load()"
      >
        <el-option
          v-for="item in clazz"
          :key="item.value"
          :label="item.name"
          :value="item.id"
        >
        </el-option>
      </el-select>
      <span style="margin-left: 20px">课程：</span>
      <el-select
        v-model="courseId"
        placeholder="请选择"
        style="width: 100px"
        clearable
        @change="load()"
      >
        <el-option
          v-for="item in course"
          :key="item.value"
          :label="item.name"
          :value="item.id"
        >
        </el-option>
      </el-select>
    </div>
    <div style="margin: 10px 0; width: 500px; float: right"></div>

    <!--  表格-->
    <el-table :data="tableData" border stripe style="width: 99%" height="72vh">
      <el-table-column type="index" width="50" label="排名" />
      <el-table-column prop="student.stuId" label="学号" sortable />
      <el-table-column prop="student.stuName" label="姓名" />
      <el-table-column prop="clazz.name" label="班级" />
      <el-table-column prop="course.name" label="课程" />
      <el-table-column prop="score" label="成绩" sortable />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button
            @click="handleEdit(scope.row)"
            size="mini"
            type="primary"
            plain
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      :title="title"
      v-model="scoreVisible"
      width="50%"
      :before-close="dialogClose"
    >
      <el-form :model="scoreForm" ref="scoreForm" :rules="rules">
        <el-row>
          <el-col span="12">
            <el-form-item label="学号" label-width="100px">
              <el-input v-model="scoreForm.student.stuId" disabled="true" />
            </el-form-item>
          </el-col>
          <el-col span="12">
            <el-form-item label="姓名" label-width="100px">
              <el-input v-model="scoreForm.student.stuName" placeholder="请输入姓名" disabled="true"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col span="12">
            <el-form-item label="班级" label-width="100px">
              <el-input v-model="scoreForm.clazz.name" disabled="true" />
            </el-form-item>
          </el-col>
          <el-col span="12">
            <el-form-item label="课程" label-width="100px">
              <el-input v-model="scoreForm.course.name" placeholder="请输入姓名" disabled="true"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col span="12">
            <el-form-item label="成绩" label-width="100px">
              <el-input v-model="scoreForm.score" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="scoreVisible = false">取 消</el-button>
          <el-button type="primary" @click="update()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
    <div style="margin: 10px 0">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import request from "../../utils/request";
export default {
  name: "List",
  data() {
    return {
      tableData: [],
      //当前页
      currentPage: 1,
      // 页大小
      pageSize: 10,
      // 总条数
      total: 0,
      // 考试id
      examInfo: {},
      studentId:'',
      // 班级下拉数据
      clazz:[],
      // 课程下拉数据
      course:[],
      scoreVisible:false,
      scoreForm:{}
    };
  },
  created() {
    this.getquery();
    this.load();
    this.getClazz()
    this.getCourse()
  },
  methods: {
    // 获取考试ID，年级id
    getquery() {
      this.examInfo = this.$route.query;
      console.log("11111");
      console.log(this.examInfo);
      console.log(this.examInfo.examId);
    },
    // 分页查询当前考试的成绩
    load() {
      request
        .get("/escore/admin/student/score", {
          params: {
            PageNum: this.currentPage,
            PageSize: this.pageSize,
            examId: this.examInfo.examId,
            studentId: this.studentId,
            clazzId: this.clazzId,
            courseId: this.courseId,
          },
        })
        .then((res) => {
          console.log(res);
          this.tableData = res.data.records;
          this.total = res.data.total;
        });
    },
    // 打开编辑框
    handleEdit(row){
      console.log("1111");
      this.scoreForm = JSON.parse(JSON.stringify(row));
      this.scoreVisible = true
    },
    update(){
       request.put("/escore/update", this.scoreForm).then((res) => {
        console.log(res);
        if (res.code === "0") {
          this.$message({
            type: "success",
            message: "更新成功",
          });
          this.scoreVisible = false;
        } else {
          this.$message({
            type: "error",
            message: res.msg,
          });
        }
        this.load();
      });
    },
    // 查询当前考试的班级
    getClazz(){
      request.get('/clazz/findClazzByGid',{
        params:{
          gradeId:this.examInfo.gradeId
        }
      }).then((res =>{
        console.log(res);
        this.clazz = res.data
      }))
    },

     // 查询当前考试的班级
    getCourse(){
      request.get('/grade/list',{
        params:{
          gradeId:this.examInfo.gradeId
        }
      }).then((res =>{
        console.log(res);
        this.course = res.data[0].courseList
      }))
    },

      //改变当前每页的个数触发
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.load();
    },
    //改变当前页码触发
    handleCurrentChange(pageNum) {
      this.currentPage = pageNum;
      this.load();
    },
  },
};
</script>

<style>
.count {
  color: #999;
  margin-top: 10px;
  font-size: 15px;
  font-weight: bold;
  text-align: center;
}
</style>